package cn.mes.module.mes.service.baseworkplan;

import cn.mes.module.mes.controller.admin.baseworkplan.BaseWorkPlanController;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.mes.module.mes.controller.admin.baseworkplan.vo.*;
import cn.mes.module.mes.dal.dataobject.baseworkplan.BaseWorkPlanDO;
import cn.mes.module.mes.dal.dataobject.baseworkplan.BaseBomChildDO;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.util.object.BeanUtils;

import cn.mes.module.mes.dal.mysql.baseworkplan.BaseWorkPlanMapper;
import cn.mes.module.mes.dal.mysql.baseworkplan.BaseBomChildMapper;

import static cn.mes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mes.module.mes.enums.ErrorCodeConstants.*;
import static cn.mes.module.system.enums.LogRecordConstants.*;

/**
 * 任务单信息 Service 实现类
 *
 * @author 游创
 */
@Service
@Validated
public class BaseWorkPlanServiceImpl implements BaseWorkPlanService {

    @Resource
    private BaseWorkPlanMapper baseWorkPlanMapper;
    @Resource
    private BaseBomChildMapper baseBomChildMapper;

    @Override
    public String createBaseWorkPlan(BaseWorkPlanSaveReqVO createReqVO) {
        // 插入
        BaseWorkPlanDO baseWorkPlan = BeanUtils.toBean(createReqVO, BaseWorkPlanDO.class);
        baseWorkPlanMapper.insert(baseWorkPlan);
        // 返回
        return baseWorkPlan.getId();
    }


    @Override
    public boolean createBaseWorkPlan(BaseWorkPlanDO baseWorkPlanDO) {
        // 插入
        baseWorkPlanMapper.insert(baseWorkPlanDO);
        // 返回
        return   baseWorkPlanMapper.insert(baseWorkPlanDO)>0;
    }


    /**
     * 创建任务单信息
     *
     * @param baseWorkPlanDOList 创建信息
     * @return 编号
     */
    @Override
    public boolean createBaseWorkPlanBatch(List<BaseWorkPlanDO> baseWorkPlanDOList) {
        return  this.baseWorkPlanMapper.insertOrUpdateBatch(baseWorkPlanDOList);
    }

    @Override
    public void updateBaseWorkPlan(BaseWorkPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseWorkPlanExists(updateReqVO.getId());
        // 更新
        BaseWorkPlanDO updateObj = BeanUtils.toBean(updateReqVO, BaseWorkPlanDO.class);
        baseWorkPlanMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBaseWorkPlan(String id) {
        // 校验存在
        validateBaseWorkPlanExists(id);
        // 删除
        baseWorkPlanMapper.deleteById(id);

        // 删除子表
        deleteBaseBomChildByWorkPlanInterId(id);
    }

    private void validateBaseWorkPlanExists(String id) {
        if (baseWorkPlanMapper.selectById(id) == null) {
            throw exception(BASE_WORK_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public BaseWorkPlanDO getBaseWorkPlan(String id) {
        return baseWorkPlanMapper.selectById(id);
    }

    @Override

    public PageResult<BaseWorkPlanDO> getBaseWorkPlanPage(BaseWorkPlanPageReqVO pageReqVO) {

        PageResult<BaseWorkPlanDO> pageResult=baseWorkPlanMapper.selectPage(pageReqVO);

        return pageResult;
    }

    // ==================== 子表（投料单详细信息） ====================

    @Override
    @LogRecord(type = MES_WORK_PLAN, subType = MES_WORK_PLAN_SUB_TYPE, bizNo = "{{#workPlanInterId}}",
            success = MES_WORK_PLAN_QUERY_BOM_SUCCESS)
    public PageResult<BaseBomChildDO> getBaseBomChildPage(PageParam pageReqVO, String workPlanInterId) {
        StopWatch stopWatch=new StopWatch();
        stopWatch.start();
        PageResult<BaseBomChildDO> pageResult= baseBomChildMapper.selectPage(pageReqVO, workPlanInterId);
        stopWatch.stop();
        LogRecordContext.putVariable("time", stopWatch.getTotalTimeSeconds());
        return pageResult;
    }

    @Override
    public String createBaseBomChild(BaseBomChildDO baseBomChild) {
        baseBomChildMapper.insert(baseBomChild);
        return baseBomChild.getId();
    }

    @Override
    public boolean createBaseBomChildBatch(List<BaseBomChildDO> baseBomChildDOList) {
        return baseBomChildMapper.insertOrUpdateBatch(baseBomChildDOList);
    }

    @Override
    public void updateBaseBomChild(BaseBomChildDO baseBomChild) {
        // 校验存在
        validateBaseBomChildExists(baseBomChild.getId());
        // 更新
        baseBomChildMapper.updateById(baseBomChild);
    }

    @Override
    public void deleteBaseBomChild(String id) {
        // 校验存在
        validateBaseBomChildExists(id);
        // 删除
        baseBomChildMapper.deleteById(id);
    }

    @Override
    public BaseBomChildDO getBaseBomChild(String id) {
        return baseBomChildMapper.selectById(id);
    }

    private void validateBaseBomChildExists(String id) {
        if (baseBomChildMapper.selectById(id) == null) {
            throw exception(BASE_BOM_CHILD_NOT_EXISTS);
        }
    }

    private void deleteBaseBomChildByWorkPlanInterId(String workPlanInterId) {
        baseBomChildMapper.deleteByWorkPlanInterId(workPlanInterId);
    }

}